package com.example.backend.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.backend.entity.BookletVO;
import com.example.backend.entity.Entry;
import com.example.backend.entity.EntryVO;
import com.example.backend.entity.WordVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * @author 李月
 * @date 2022/5/2 19:07
 */
@Mapper
public interface EntryMapper extends BaseMapper<Entry> {
    /**
     * @param bookletId 单词本id
     * @return
     */
    @Select("SELECT COUNT(tb_booklet_entry.entry_id) FROM tb_entry,tb_booklet_entry WHERE tb_entry.study_count>0 AND tb_entry.entry_id=tb_booklet_entry.entry_id AND tb_booklet_entry.booklet_id=#{bookletId}")
    int getEntryLearnedCount(@Param("bookletId") int bookletId);

    /**
     * @param bookletId
     * @param page
     * @return
     */
    @Select("SELECT tb_word.word_spell AS wordSpell,tb_entry.study_count AS learnedCount,tb_entry.last_study_datetime AS lastLearnedTime FROM tb_word,tb_entry,tb_booklet_entry WHERE tb_booklet_entry.booklet_id=#{bookletId} AND tb_booklet_entry.entry_id=tb_entry.entry_id AND tb_word.word_id=tb_entry.entry_word_id")
    List<EntryVO> getEntry(@Param("bookletId") int bookletId, Page<EntryVO> page);

    /**
     * @param entryId
     * @return
     */
    @Select("SELECT entry_word_id FROM tb_entry WHERE tb_entry.entry_id=#{entryId}")
    int getWordId(@Param("entryId") int entryId);

    /**
     * @param bookletId
     * @return
     */
    @Select("SELECT tb_entry.entry_id AS entryId,tb_entry.study_count AS studyCount,tb_word.word_spell AS wordSpell,tb_word.soundmark,tb_word.paraphrase,tb_entry.remark FROM tb_booklet_entry,tb_entry,tb_word WHERE tb_booklet_entry.booklet_id=#{bookletId} AND tb_entry.entry_id=tb_booklet_entry.entry_id AND tb_entry.entry_word_id=tb_word.word_id ORDER BY tb_entry.last_study_datetime LIMIT 20")
    List<WordVO> getTwentyWords(@Param("bookletId") int bookletId);
}
